<?php
namespace Home\Controller;
use Think\Controller;

class CrontabController extends Controller {
	/*
	* 获取跨境购订单状态 指定时间段
	*/
	public function cancelCourse(){
		try{
			set_time_limit(0);
			$today = date('Y-m-d',time());
			$time = time();
			$list = M('course')->where(['course_date'=>$today,'status'=>1,'is_deleted'=>0])->select();
			if(is_array($list)&&count($list)>0){
			    foreach($list as $key=>$val){
			        $courseTime = strtotime($val['course_date'].' '.$val['begin_time']);
			        $courseTime = strtotime($courseTime);
			        if(($time-$courseTime)>=0){
			            //获取已预约人数
			            $hasCoursed = M('user_course')->where(['course_id'=>$val['id'],'status'=>1,'is_deleted'=>0])->count();
			            if(empty($val['min_order_nums'])){
			               $val['min_order_nums'] = $val['nums']; 
			            }
			            if(($hasCoursed-$val['min_order_nums'])<0){
			                $list = M('user_course')->where(['course_id'=>$val['id'],'status'=>['in',[1,3]],'is_deleted'=>0])->select();
			                if(is_array($list)&&count($list)>0){
			                    foreach($list as $k=>$v){
			                        D('UserCourse')->startTrans();
			                        $ret = D('UserCourse')->cancelUserCourse($v['id'],2);
                                    if($ret['code']==0){
                                        D('UserCourse')->rollback();
                                        var_dump($ret);
                                    }
                                    D('UserCourse')->commit();
			                    }
			                }
			            }
			        }
			    }
			}
		}catch(Exception $e){
			echo $e->getMessage();
			exit();
		}
	}
}